﻿using System;
using System.Data;
using System.Text;
//using System.Data.SqlClient;
using MySql.Data.MySqlClient;
using WeFrame.Core.DBUtility;
using WeFrame.Core.Common;
using WeFrame.Core.Model;

namespace WeFrame.Core.DAL
{
	/// <summary>
	/// 数据访问类:后台导航菜单
	/// </summary>
	public partial class SystemMenuDAL
	{


        #region 基本方法===============================
        /// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select count(1) from System_Menu");
			strSql.Append(" where Id=@id");
			MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}

        /// <summary>
        /// 查询是否存在该记录
        /// </summary>
        public bool Exists(string name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from System_Menu");
            strSql.Append(" where name=@name ");
            MySqlParameter[] parameters = {
					new MySqlParameter("@name", MySqlDbType.VarChar,50)};
            parameters[0].Value = name;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(SystemMenuEntity model)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("insert into System_Menu(");
			strSql.Append("ParentId,ChannelId,NavType,Name,Title,SubTitle,IconUrl,LinkUrl,SortId,IsLock,Remark,ActionType,IsSys)");
			strSql.Append(" values (");
			strSql.Append("@parentId,@channelId,@navType,@name,@title,@subTitle,@iconUrl,@linkUrl,@sortId,@isLock,@remark,@actionType,@isSys)");
			strSql.Append(";select @@IDENTITY");
			MySqlParameter[] parameters = {
					new MySqlParameter("@parentId", MySqlDbType.Int32,4),
					new MySqlParameter("@channelId", MySqlDbType.Int32,4),
					new MySqlParameter("@navType", MySqlDbType.VarChar,50),
					new MySqlParameter("@name", MySqlDbType.VarChar,50),
					new MySqlParameter("@title", MySqlDbType.VarChar,100),
					new MySqlParameter("@subTitle", MySqlDbType.VarChar,100),
					new MySqlParameter("@iconUrl", MySqlDbType.VarChar,255),
					new MySqlParameter("@linkUrl", MySqlDbType.VarChar,255),
					new MySqlParameter("@sortId", MySqlDbType.Int32,4),
					new MySqlParameter("@isLock", MySqlDbType.Byte,1),
					new MySqlParameter("@remark", MySqlDbType.VarChar,500),
					new MySqlParameter("@actionType", MySqlDbType.VarChar,500),
					new MySqlParameter("@isSys", MySqlDbType.Byte,1)};
			parameters[0].Value = model.ParentId;
			parameters[1].Value = model.ChannelId;
			parameters[2].Value = model.NavType;
			parameters[3].Value = model.Name;
			parameters[4].Value = model.Title;
			parameters[5].Value = model.SubTitle;
			parameters[6].Value = model.IconUrl;
			parameters[7].Value = model.LinkUrl;
			parameters[8].Value = model.SortId;
			parameters[9].Value = model.IsLock;
			parameters[10].Value = model.Remark;
			parameters[11].Value = model.ActionType;
			parameters[12].Value = model.IsSys;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(SystemMenuEntity model)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("update System_Menu set ");
			strSql.Append("ParentId=@parentId,");
			strSql.Append("ChannelId=@channelId,");
			strSql.Append("NavType=@navType,");
			strSql.Append("Name=@name,");
			strSql.Append("Title=@title,");
			strSql.Append("SubTitle=@subTitle,");
			strSql.Append("IconUrl=@iconUrl,");
			strSql.Append("LinkUrl=@linkUrl,");
			strSql.Append("SortId=@sortId,");
			strSql.Append("IsLock=@isLock,");
			strSql.Append("Remark=@remark,");
			strSql.Append("ActionType=@actionType,");
			strSql.Append("IsSys=@isSys");
			strSql.Append(" where Id=@id");
			MySqlParameter[] parameters = {
					new MySqlParameter("@parentId", MySqlDbType.Int32,4),
					new MySqlParameter("@channelId", MySqlDbType.Int32,4),
					new MySqlParameter("@navType", MySqlDbType.VarChar,50),
					new MySqlParameter("@name", MySqlDbType.VarChar,50),
					new MySqlParameter("@title", MySqlDbType.VarChar,100),
					new MySqlParameter("@subTitle", MySqlDbType.VarChar,100),
					new MySqlParameter("@iconUrl", MySqlDbType.VarChar,255),
					new MySqlParameter("@linkUrl", MySqlDbType.VarChar,255),
					new MySqlParameter("@sortId", MySqlDbType.Int32,4),
					new MySqlParameter("@isLock", MySqlDbType.Byte,1),
					new MySqlParameter("@remark", MySqlDbType.VarChar,500),
					new MySqlParameter("@actionType", MySqlDbType.VarChar,500),
					new MySqlParameter("@isSys", MySqlDbType.Byte,1),
					new MySqlParameter("@id", MySqlDbType.Int32,4)};
			parameters[0].Value = model.ParentId;
			parameters[1].Value = model.ChannelId;
			parameters[2].Value = model.NavType;
			parameters[3].Value = model.Name;
			parameters[4].Value = model.Title;
			parameters[5].Value = model.SubTitle;
			parameters[6].Value = model.IconUrl;
			parameters[7].Value = model.LinkUrl;
			parameters[8].Value = model.SortId;
			parameters[9].Value = model.IsLock;
			parameters[10].Value = model.Remark;
			parameters[11].Value = model.ActionType;
			parameters[12].Value = model.IsSys;
			parameters[13].Value = model.Id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from System_Menu");
            strSql.Append(" where id in(" + GetIds(id) + ")");

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public SystemMenuEntity GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,ParentId,ChannelId,NavType,Name,Title,SubTitle,IconUrl,LinkUrl,SortId,IsLock,Remark,ActionType,IsSys");
            strSql.Append(" from System_Menu ");
            strSql.Append(" where Id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32,4)};
            parameters[0].Value = id;

            SystemMenuEntity model = new SystemMenuEntity();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SystemMenuEntity GetModel(string navName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  Id,ParentId,ChannelId,NavType,Name,Title,SubTitle,IconUrl,LinkUrl,SortId,IsLock,Remark,ActionType,IsSys");
            strSql.Append(" from System_Menu ");
            strSql.Append(" where Name=@navName");
            strSql.Append(" limit 1");
            MySqlParameter[] parameters = {
					new MySqlParameter("@navName", MySqlDbType.VarChar,50)};
            parameters[0].Value = navName;

            SystemMenuEntity model = new SystemMenuEntity();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取类别列表
        /// </summary>
        /// <param name="parent_id">父ID</param>
        /// <param name="nav_type">导航类别</param>
        /// <returns>DataTable</returns>
        public DataTable GetList(int parentId, string navType)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,ParentId,ChannelId,NavType,Name,Title,SubTitle,IconUrl,LinkUrl,SortId,IsLock,Remark,ActionType,IsSys");
            strSql.Append(" FROM System_Menu");
            strSql.Append(" where NavType='" + navType + "'");
            strSql.Append(" order by SortId asc,Id desc");
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            //重组列表
            DataTable oldData = ds.Tables[0] as DataTable;
            if (oldData == null)
            {
                return null;
            }
            //创建一个新的DataTable增加一个深度字段
            DataTable newData = new DataTable();
            newData.Columns.Add("Id", typeof(int));
            newData.Columns.Add("ParentId", typeof(int));
            newData.Columns.Add("ChannelId", typeof(int));
            newData.Columns.Add("ClassLayer", typeof(int));
            newData.Columns.Add("NavType", typeof(string));
            newData.Columns.Add("Name", typeof(string));
            newData.Columns.Add("Title", typeof(string));
            newData.Columns.Add("SubTitle", typeof(string));
            newData.Columns.Add("IconUrl", typeof(string));
            newData.Columns.Add("LinkUrl", typeof(string));
            newData.Columns.Add("SortId", typeof(int));
            newData.Columns.Add("IsLock", typeof(int));
            newData.Columns.Add("Remark", typeof(string));
            newData.Columns.Add("ActionType", typeof(string));
            newData.Columns.Add("IsSys", typeof(int));
            //调用迭代组合成DAGATABLE
            GetChilds(oldData, newData, parentId, 0);
            return newData;
        }
		#endregion

        #region 扩展方法===============================
        /// <summary>
        /// 根据导航的名称查询其ID
        /// </summary>
        public int GetNavId(string navName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id from System_Menu");
            strSql.Append(" where Name=@navName");
            strSql.Append(" limit 1");
            MySqlParameter[] parameters = {
					new MySqlParameter("@navName", MySqlDbType.VarChar,50)};
            parameters[0].Value = navName;
            string str = Convert.ToString(DbHelperSQL.GetSingle(strSql.ToString(), parameters));
            return Utils.StrToInt(str, 0);
        }

        /// <summary>
        /// 获取父类下的所有子类ID(含自己本身)
        /// </summary>
        public string GetIds(int parentId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,ParentId");
            strSql.Append(" FROM System_Menu");
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            string ids = parentId.ToString() + ",";
            GetChildIds(ds.Tables[0], parentId, ref ids);
            return ids.TrimEnd(',');
        }

        /// <summary>
        /// 获取父类下的所有子类ID(含自己本身)
        /// </summary>
        public string GetIds(string navName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  id from SystemMenu");
            strSql.Append(" where name=@navName");
            strSql.Append(" limit 1");
            MySqlParameter[] parameters = {
					new MySqlParameter("@navName", MySqlDbType.VarChar,50)};
            parameters[0].Value = navName;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj != null)
            {
                return GetIds(Convert.ToInt32(obj));
            }
            return string.Empty;
        }

        /// <summary>
        /// 修改一列数据
        /// </summary>
        public bool UpdateField(int id, string strValue)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update System_Menu set " + strValue);
            strSql.Append(" where Id=" + id);
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 修改一列数据
        /// </summary>
        public bool UpdateField(string name, string strValue)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update System_Menu set " + strValue);
            strSql.Append(" where Name='" + name + "'");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 修改一条记录，带事务
        /// </summary>
        public bool Update(MySqlConnection conn, MySqlTransaction trans, string oldName, string newName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update System_Menu set name=@newName");
            strSql.Append(" where name=@oldName");
            MySqlParameter[] parameters = {
					            new MySqlParameter("@newName", MySqlDbType.VarChar,50),
					            new MySqlParameter("@oldName", MySqlDbType.VarChar,50)};
            parameters[0].Value = newName;
            parameters[1].Value = oldName;
            int rows = DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 修改一条记录，带事务
        /// </summary>
        public bool Update(MySqlConnection conn, MySqlTransaction trans, string oldName, string newName, string title, int sortId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update System_Menu set");
            strSql.Append(" Name=@newName,");
            strSql.Append(" Title=@title,");
            strSql.Append(" SortId=@sortId");
            strSql.Append(" where name=@oldName");
            MySqlParameter[] parameters = {
					new MySqlParameter("@newName", MySqlDbType.VarChar,50),
                    new MySqlParameter("@title", MySqlDbType.VarChar,100),
                    new MySqlParameter("@sortId", MySqlDbType.Int32,4),
                    new MySqlParameter("@oldName", MySqlDbType.VarChar,50)};
            parameters[0].Value = newName;
            parameters[1].Value = title;
            parameters[2].Value = sortId;
            parameters[3].Value = oldName;
            int rows = DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 修改一条记录，带事务
        /// </summary>
        public bool Update(MySqlConnection conn, MySqlTransaction trans, string oldName, int parentId, string navName, string title, int sortId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update System_Menu set");
            strSql.Append(" ParentId=@parentId,");
            strSql.Append(" Name=@name,");
            strSql.Append(" Title=@title,");
            strSql.Append(" SortId=@sortId");
            strSql.Append(" where Name=@oldName");
            MySqlParameter[] parameters = {
					new MySqlParameter("@parentId", MySqlDbType.Int32,4),
					new MySqlParameter("@name", MySqlDbType.VarChar,50),
                    new MySqlParameter("@title", MySqlDbType.VarChar,100),
                    new MySqlParameter("@sortId", MySqlDbType.Int32,4),
                    new MySqlParameter("@oldName", MySqlDbType.VarChar,50)};
            parameters[0].Value = parentId;
            parameters[1].Value = navName;
            parameters[2].Value = title;
            parameters[3].Value = sortId;
            parameters[4].Value = oldName;
            int rows = DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 快捷添加系统默认导航
        /// </summary>
        public int Add(string parentName, string navName, string title, string linkUrl, int sortId, int channelId, string actionType)
        {
            //先根据名称查询该父ID
            StringBuilder strSql1 = new StringBuilder();
            strSql1.Append("select id from System_Menu");
            strSql1.Append(" where Name=@parentName");
            strSql1.Append(" limit 1");
            MySqlParameter[] parameters1 = {
					new MySqlParameter("@parentName", MySqlDbType.VarChar,50)};
            parameters1[0].Value = parentName;
            object obj1 = DbHelperSQL.GetSingle(strSql1.ToString(), parameters1);
            if (obj1 == null)
            {
                return 0;
            }
            int parentId = Convert.ToInt32(obj1);

            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into System_Menu(");
            strSql.Append("ParentId,ChannelId,NavType,Name,Title,LinkUrl,SortId,ActionType,IsLock,IsSys)");
            strSql.Append(" values (");
            strSql.Append("@parentId,@channelId,@navType,@name,@title,@linkUrl,@sortId,@actionType,@isLock,@isSys)");
            strSql.Append(";select @@IDENTITY");
            MySqlParameter[] parameters = {
					new MySqlParameter("@parentId", MySqlDbType.Int32,4),
					new MySqlParameter("@channelId", MySqlDbType.Int32,4),
					new MySqlParameter("@navType", MySqlDbType.VarChar,50),
					new MySqlParameter("@name", MySqlDbType.VarChar,50),
					new MySqlParameter("@title", MySqlDbType.VarChar,100),
					new MySqlParameter("@linkUrl", MySqlDbType.VarChar,255),
					new MySqlParameter("@sortId", MySqlDbType.Int32,4),
					new MySqlParameter("@actionType", MySqlDbType.VarChar,500),
					new MySqlParameter("@isLock", MySqlDbType.Byte,1),
					new MySqlParameter("@isSys", MySqlDbType.Byte,1)};
            parameters[0].Value = parentId;
            parameters[1].Value = channelId;
            parameters[2].Value = WeFrameEnums.MenuEnum.System.ToString();
            parameters[3].Value = navName;
            parameters[4].Value = title;
            parameters[5].Value = linkUrl;
            parameters[6].Value = sortId;
            parameters[7].Value = actionType;
            parameters[8].Value = 0;
            parameters[9].Value = 1;
            object obj2 = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            return Convert.ToInt32(obj2);
        }

        /// <summary>
        /// 快捷添加系统默认导航，带事务
        /// </summary>
        public int Add(MySqlConnection conn, MySqlTransaction trans, string parentName, string navName, string title, string linkUrl, int sortId, int channelId, string actionType)
        {
            //先根据名称查询该父ID
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  id from System_Menu");
            strSql.Append(" where Name=@parentName");
            strSql.Append(" limit 1");
            MySqlParameter[] parameters = {
					new MySqlParameter("@parentName", MySqlDbType.VarChar,50)};
            parameters[0].Value = parentName;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            int parent_id = Convert.ToInt32(obj);

            return Add(conn, trans, parent_id, navName, title, linkUrl, sortId, channelId, actionType);
        }

        /// <summary>
        /// 快捷添加系统默认导航，带事务
        /// </summary>
        public int Add(MySqlConnection conn, MySqlTransaction trans, int parentId, string navName, string title, string linkUrl, int sortId, int channelId, string actionType)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into System_Menu(");
            strSql.Append("ParentId,ChannelId,NavType,Name,Title,LinkUrl,SortId,ActionType,IsLock,IsSys)");
            strSql.Append(" values (");
            strSql.Append("@parentId,@channelId,@navType,@name,@title,@linkUrl,@sortId,@actionType,@isLock,@isSys)");
            strSql.Append(";select @@IDENTITY");
            MySqlParameter[] parameters = {
					new MySqlParameter("@parentId", MySqlDbType.Int32,4),
					new MySqlParameter("@channelId", MySqlDbType.Int32,4),
					new MySqlParameter("@navType", MySqlDbType.VarChar,50),
					new MySqlParameter("@name", MySqlDbType.VarChar,50),
					new MySqlParameter("@title", MySqlDbType.VarChar,100),
					new MySqlParameter("@linkUrl", MySqlDbType.VarChar,255),
					new MySqlParameter("@sortId", MySqlDbType.Int32,4),
					new MySqlParameter("@actionType", MySqlDbType.VarChar,500),
					new MySqlParameter("@isLock", MySqlDbType.Byte,1),
					new MySqlParameter("@isSys", MySqlDbType.Byte,1)};
            parameters[0].Value = parentId;
            parameters[1].Value = channelId;
            parameters[2].Value = WeFrameEnums.MenuEnum.System.ToString();
            parameters[3].Value = navName;
            parameters[4].Value = title;
            parameters[5].Value = linkUrl;
            parameters[6].Value = sortId;
            parameters[7].Value = actionType;
            parameters[8].Value = 0;
            parameters[9].Value = 1;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            return Convert.ToInt32(obj);
        }

        /// <summary>
        /// 得到一个对象实体，带事务
        /// </summary>
        public SystemMenuEntity GetModel(MySqlConnection conn, MySqlTransaction trans, string navName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  Id,ParentId,ChannelId,NavType,Name,Title,SubTitle,IconUrl,LinkUrl,SortId,IsLock,Remark,ActionType,IsSys");
            strSql.Append(" from System_Menu ");
            strSql.Append(" where Name=@navName");
            strSql.Append(" limit 1");
            MySqlParameter[] parameters = {
					new MySqlParameter("@navName", MySqlDbType.VarChar,50)};
            parameters[0].Value = navName;

            SystemMenuEntity model = new SystemMenuEntity();
            DataSet ds = DbHelperSQL.Query(conn, trans, strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 删除一条数据，带事务
        /// </summary>
        public bool Delete(MySqlConnection conn, MySqlTransaction trans, string navName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from System_Menu");
            strSql.Append(" where Name=@name");
            MySqlParameter[] parameters = {
					new MySqlParameter("@name", MySqlDbType.VarChar,50)};
            parameters[0].Value = navName;

            int rows = DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 私有方法===============================
        /// <summary>
        /// 组合成对象实体
        /// </summary>
        private SystemMenuEntity DataRowToModel(DataRow row)
        {
            SystemMenuEntity model = new SystemMenuEntity();
            if (row != null)
            {
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (row["ParentId"] != null && row["ParentId"].ToString() != "")
                {
                    model.ParentId = int.Parse(row["ParentId"].ToString());
                }
                if (row["ChannelId"] != null && row["ChannelId"].ToString() != "")
                {
                    model.ChannelId = int.Parse(row["ChannelId"].ToString());
                }
                if (row["NavType"] != null)
                {
                    model.NavType = row["NavType"].ToString();
                }
                if (row["Name"] != null)
                {
                    model.Name = row["Name"].ToString();
                }
                if (row["Title"] != null)
                {
                    model.Title = row["Title"].ToString();
                }
                if (row["SubTitle"] != null)
                {
                    model.SubTitle = row["SubTitle"].ToString();
                }
                if (row["IconUrl"] != null)
                {
                    model.IconUrl = row["IconUrl"].ToString();
                }
                if (row["LinkUrl"] != null)
                {
                    model.LinkUrl = row["LinkUrl"].ToString();
                }
                if (row["SortId"] != null && row["SortId"].ToString() != "")
                {
                    model.SortId = int.Parse(row["SortId"].ToString());
                }
                if (row["IsLock"] != null && row["IsLock"].ToString() != "")
                {
                    model.IsLock = byte.Parse(row["IsLock"].ToString());
                }
                if (row["Remark"] != null)
                {
                    model.Remark = row["Remark"].ToString();
                }
                if (row["ActionType"] != null)
                {
                    model.ActionType = row["ActionType"].ToString();
                }
                if (row["IsSys"] != null && row["IsSys"].ToString() != "")
                {
                    model.IsSys = byte.Parse(row["IsSys"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 从内存中取得所有下级类别列表（自身迭代）
        /// </summary>
        private void GetChilds(DataTable oldData, DataTable newData, int parentId, int classLayer)
        {
            classLayer++;
            DataRow[] dr = oldData.Select("parentId=" + parentId);
            for (int i = 0; i < dr.Length; i++)
            {
                //添加一行数据
                DataRow row = newData.NewRow();
                row["Id"] = int.Parse(dr[i]["Id"].ToString());
                row["ParentId"] = int.Parse(dr[i]["ParentId"].ToString());
                row["ChannelId"] = int.Parse(dr[i]["ChannelId"].ToString());
                row["ClassLayer"] = classLayer;
                row["NavType"] = dr[i]["NavType"].ToString();
                row["Name"] = dr[i]["Name"].ToString();
                row["Title"] = dr[i]["Title"].ToString();
                row["SubTitle"] = dr[i]["SubTitle"].ToString();
                row["IconUrl"] = dr[i]["IconUrl"].ToString();
                row["LinkUrl"] = dr[i]["LinkUrl"].ToString();
                row["SortId"] = int.Parse(dr[i]["SortId"].ToString());
                row["IsLock"] = int.Parse(dr[i]["IsLock"].ToString());
                row["Remark"] = dr[i]["Remark"].ToString();
                row["ActionType"] = dr[i]["ActionType"].ToString();
                row["IsSys"] = int.Parse(dr[i]["IsSys"].ToString());
                newData.Rows.Add(row);
                //调用自身迭代
                this.GetChilds(oldData, newData, int.Parse(dr[i]["Id"].ToString()), classLayer);
            }
        }

        /// <summary>
        /// 获取父类下的所有子类ID
        /// </summary>
        private void GetChildIds(DataTable dt, int parentId, ref string ids)
        {
            DataRow[] dr = dt.Select("ParentId=" + parentId);
            for (int i = 0; i < dr.Length; i++)
            {
                ids += dr[i]["Id"].ToString() + ",";
                //调用自身迭代
                this.GetChildIds(dt, int.Parse(dr[i]["Id"].ToString()), ref ids);
            }
        }
        #endregion
    }
}

